//
//  MusicPlayerViewController.m
//  MJAVFoundationDemo
//
//  Created by apple on 16/9/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MusicPlayerViewController.h"
#import "MusicListCell.h"
#import "PlayerPresentaionViewController.h"
@interface MusicPlayerViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _selectedRow;//保存哪一行被选中
}
@property(nonatomic,strong)UITableView *mainTableView;//创建一个表
@property(nonatomic,strong)NSMutableArray *dataSource;//创建表的数据源

@end

@implementation MusicPlayerViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    _selectedRow=-1;
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"音乐播放器";
    _dataSource=[NSMutableArray array];
    [self getData];
    [self.view addSubview:self.mainTableView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeBorder:) name:@"changeBorder" object:nil];
    
}
#pragma mark--收到自动播放下一首的通知时刷新表，圈出正在播放的音乐
-(void)changeBorder:(NSNotification *)notify
{
    NSNumber *number=notify.object;
    _selectedRow=number.integerValue;
    [_mainTableView reloadData];
}
#pragma mark--获取数据
-(void)getData
{
    NSMutableArray *iconArr=[MJPlayerTool getDataWithType:nil andSuffixName:@"icon@2x"];
    NSArray *singerArr=[MJPlayerTool getSingerArr];
    NSArray *songArr=[MJPlayerTool getSongArr];
    NSArray *bgImgArr=[MJPlayerTool getBgImgArr];
  
    for (int i=0; i<iconArr.count; i++) {
        MusicPlayerModel *model=[[MusicPlayerModel alloc]init];
        model.bgImageName=bgImgArr[i];
        model.IconName=iconArr[i];
        model.singerName=singerArr[i];
        model.musicName=songArr[i];
        [MJPlayerTool paserLrcWithMusicPath:[NSString stringWithFormat:@"%@.lrc",model.musicName] finished:^(NSMutableArray *musicTime, NSMutableArray *content) {
            model.lrcTimeArray=musicTime;
            model.lrcsArray=content;
        }];
        [_dataSource addObject:model];
    }
    
}

//懒加载的方式初始化表，只有self.打点调用时才会执行到这里
-(UITableView *)mainTableView
{
    if (!_mainTableView)
    {
        _mainTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, KWIDTH, KHEIGHT-64)];
        //设置表代理才能用它的代理方法
        _mainTableView.delegate=self;
        _mainTableView.dataSource=self;
        _mainTableView.rowHeight=70;
        _mainTableView.tableFooterView=[UIView new];
        //注册单元格
        [_mainTableView registerNib:[UINib nibWithNibName:@"MusicListCell" bundle:nil] forCellReuseIdentifier:@"reuseCell"];
    }
    return _mainTableView;
}
//表代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicListCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuseCell" forIndexPath:indexPath];
    MusicPlayerModel *model=_dataSource[indexPath.row];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.headImage.image=[UIImage imageNamed:model.IconName];
    cell.titleLbl.text=model.musicName;
    cell.singerLbl.text=model.singerName;
    //改变图片正在播放与等待播放的状态
    if (indexPath.row==_selectedRow) {
        [cell changeBorderCorlorIsPlaying];
    }else
    {
        [cell changeBorderCorlorOnWaiting];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectedRow=indexPath.row;
    //到播放视图
    MusicPlayerModel *model=_dataSource[indexPath.row];
    PlayerPresentaionViewController *player=[PlayerPresentaionViewController SharedPlayer];
    player.song=model.musicName;
    player.songIndex=indexPath.row;
    player.allSongsDataArray=_dataSource;
    [self presentViewController:player animated:YES completion:nil];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_mainTableView reloadData];
}
@end
